﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace Dragon.Prototype1.Infrastructure.Controls
{
    public class TextBox : jQueryControl
    {
        public TextBox(string id):this(id,"")
        {
            
        }

        public TextBox(string id, string text):this(id,text,false,false)
        {
        }

        public TextBox(string id, bool isMultiLine,bool isPasswordBox):this(id,"",isMultiLine,isPasswordBox)
        {
        }

        public TextBox(string id, string text, bool isMultiLine,bool isPasswordBox)
        {
            this.ID = id;
            this.Name = id;
            this.Text = text;
            this.IsMultiLine = isMultiLine;
            this.IsPasswordBox = isPasswordBox;
        }

        public string Text
        {
            get;
            set;
        }

        public bool IsPasswordBox
        {
            get;
            set;
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                if (this.IsMultiLine)
                {
                    return HtmlTextWriterTag.Textarea;
                }
                return HtmlTextWriterTag.Input;
            }
        }
        public bool IsReadOnly
        {
            get;
            set;
        }
        public bool IsMultiLine
        {
            get;
            set;
        }
        public int Rows
        {
            get;
            set;
        }
        public int Columns
        {
            get;
            set;
        }
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            if (!IsMultiLine)
            {
                if (!string.IsNullOrEmpty(this.Text))
                {
                    //writer.AddAttribute(HtmlTextWriterAttribute.Value, Microsoft.Security.Application.AntiXss.HtmlEncode(this.Text));
                    writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
                }
                if (IsPasswordBox)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Type, "password");
                }
                else
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
                }
            }
            else
            {
                if (this.Rows > 0)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Rows, this.Rows.ToString());
                }
                if (this.Columns > 0)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Cols, this.Columns.ToString());
                }

            }
            if (IsReadOnly)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.ReadOnly, "true");
            }

            base.AddAttributesToRender(writer);
        }
        public override void RenderContents(HtmlTextWriter writer)
        {
            if (IsMultiLine)
            {
                writer.Write(Microsoft.Security.Application.AntiXss.HtmlEncode(this.Text));
                //writer.Write(this.Text);
            }
        }
    }
}
